/**
 * @author Robert_Csala
 */

var createState = function(userid, testNumber){
    var state = {
        values: {},
        delta: undefined,
        submitDeltaCount: 0,
        submitDelta: function (delta) {
            this.delta = delta;
            this.submitDeltaCount += 1;
        },
        submitValue: function (key, value) {
            this.delta = {};
            this.delta[key] = value;
            this.submitDeltaCount += 1;
        },
        getKeysCount: 0,
        keys: [],
        getKeys: function () {
            this.getKeysCount += 1;
            return this.keys;
        },
        setDelta: function (delta) {
            this.delta = delta;
            this.keys = [];
            for(var key in delta) {
                this.keys.push(key);
            }
        },
        get: function (key, defaultValue) {
            return (this.values[key])?this.values[key]:defaultValue;
        }
    };
    if(userid) {
        state.keys = [userid + testNumber, "other2", userid + (testNumber - 1)];
    } else {
        state.keys = [];
    }

    function addValues() {
        var stateKeys = state.getKeys();
        state.values = {};
        for (var i = 0; i < stateKeys.length; i++) {
            var key = stateKeys[i];
            var tempObject = {
                id: key,
                text: key + " text",
                value: key + " value"
                };
            state.values[key] = JSON.stringify(tempObject);
        }
    }

    state.setKeys = function (keySet) {
        if(keySet) {
            var keyType = typeof keySet;
            if(keyType === 'string' || keyType === 'number' || keyType === 'boolean' || keyType === 'date') {
                this.keys = [keySet];
            } else {
                this.keys = [];
                for(var ndx in keySet) {
                    var key = keySet[ndx];
                    this.keys.push(key);
                }
            }
            addValues();
        }
    }
    return state;
}

if(!gadgets) {
    var gadgets = {
        util: {
            registerOnLoadHandlerCount: 0,
            registerOnLoadHandler: function (f) {
                this.registerOnLoadHandler += 1;
            }
        }
    };
}

var wave = {
    participant : {
        id: "testName@wave.google.com",
        getId: function() {
            return this.id;
        },
        setId: function(newId) {
            this.id = newId;
        }
    },
    getViewer: function () {
        return this.participant;
    },
    state: createState(),
    getStateCount: 0,
    getState: function () {
        this.getStateCount += 1;
        return this.state;
    },
    isInWaveContainerCount: 0,
    isInWaveContainer: function () {
        this.isInWaveContainerCount += 1;
        return true;
    },
    setStateCallbackCount: 0,
    callbackFunction: undefined,
    setStateCallback: function (callbackFunction) {
        this.setStateCallbackCount += 1;
        this.callbackFunction = callbackFunction;
    }
};

var createUpdateFunction = function (that, registerFunction, text) {

    return function() {
        return registerFunction(that, text, arguments);
    };
}

var Raphael = function () {
    Raphael.called = 0;
    return paper;
};

var paper = {
    register: {},
    params: {},
    node: {},
    generalOrigamiObject: {id: "general"},
    types: ["text", "rect", "path", "set", "image", "animate", ],
    clean: function () {
        this.register = {};
        this.params = {};
    }
};
paper.registerFunction = function (that, key, args) {
        if(that.register[key]) {
            that.register[key]++;
        } else {
            that.register[key] = 1;
        }
        that.params[key] = args;
        return that;
    }
for(var key in paper.types) {
    paper[paper.types[key]] = createUpdateFunction(paper, paper.registerFunction, paper.types[key]);
}

var createTheory = function(){
    return {
        stateHandlerCount: 0,
        statePassed: undefined,
        stateHandler: function(state){
            this.statePassed = state;
            this.stateHandlerCount += 1;
        }
    }
};

var createConsole = function(){
    var console = {
        msg: "",
        count: 0,
        log: function(message){
            this.count++;
            this.msg = message;
        },
        getCount: function(){
            return this.count;
        },
        getMessage: function(){
            return this.msg;
        }
    };
    return console;
};
